import { request } from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeType: 'COURSE',
    activeStatus: 'WAIT',
    orderData: [],
    page: 1,
    size: 10,
    showNoMore: false,
    showLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOrderList()
  },

  confirmOrder(item) {
    request({
      url: '/mini/api/mine/confirmOrder',
      method: 'POST',
      data: {
        orderId: Number(
          item.currentTarget.dataset.item.orderId
        )
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.showToast({
          title: '确认收货成功'
        })
        this.setData({
          page: 1,
          size: 10
        })
        this.getOrderList()
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  getOrderList() {
    request({
      url: '/mini/api/mine/queryOrder',
      method: 'POST',
      data: {
        page: this.data.page,
        size: this.data.size,
        orderType: this.data.activeType
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        let newData = this.data.showLoading
          ? this.data.orderData.concat(
              resdata.data.data
            )
          : resdata.data.data
        let showNoMore =
          resdata.data.data.length < 10
        this.setData({
          orderData: newData || [],
          showLoading: false,
          showNoMore: showNoMore
        })
      } else {
        this.setData({
          orderData: [],
          showLoading: false,
          showNoMore: false
        })
      }
    })
  },

  onChangeType(event) {
    this.setData({
      activeType: event.detail.name,
      page: 1,
      size: 10
    })
    this.getOrderList()
  },

  onChangeStatus(event) {
    this.setData({
      activeStatus: event.detail.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (
      this.data.showLoading ||
      this.data.showNoMore
    ) {
      return
    }
    let newPage = this.data.page + 1
    this.setData({
      showLoading: true,
      page: newPage
    })
    this.getOrderList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
